package cn.controller;

import cn.entity.Goods;
import cn.entity.Kind;
import cn.service.GoodsService;
import cn.service.KindService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    KindService kindService;
    @Autowired
    GoodsService goodsService;
    @RequestMapping("/index")
    public String index(Integer kid, String name, Integer pageNo, Model model, HttpSession session){


        if(pageNo==null){
            pageNo=1;
        }
        //数量
        PageHelper.startPage(pageNo,8);
        List<Goods> list=goodsService.find(kid,name);
        for(Goods v:list){
            if(v.getKid()!=null){
                Kind kind=kindService.selectByPrimaryKey(v.getKid());
                v.setKind(kind);
            }
        }
        PageInfo<Goods> pageInfo=new PageInfo<Goods>(list);
        if(list.size()>3){
            List<Goods> hotList=list.subList(0, 3);
            session.setAttribute("hotList", hotList);
        }

        model.addAttribute("name", name);
        model.addAttribute("pageInfo", pageInfo);
        //查询分类
        List<Kind> kindList=kindService.find(null, null);
        session.setAttribute("kindList", kindList);
        return "index";
    }
}
